package resultEditor.relationship.complex;

import commons.Tools;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JList;
import resultEditor.annotations.Annotation;
import resultEditor.workSpace.WorkSet;
import userInterface.GUI;

/**
 * A dialog that used to select annotations and relationship from the lists
 * to create new relationship for a annotation pair.
 * 
 * At some situations, one click may select multiple annotations, and one 
 * annotation pair may have more than one suitable relationship, this dialog
 * is user to help us handle this situation. It list all possible situation
 * for user to make the final decision.
 * 
 * It only got the annotations (from parameter: "_rel_checking_result") which are 
 * verified to have capability to create relationship with others.
 * 
 * 
 * @author Jianwei Leng, March 20, 2012
 */
public class RelationshipBuilder extends javax.swing.JFrame {

    /**the instance of class "GUI"; it the main GUI frame of eHOST.*/
    private userInterface.GUI __gui = null;
    /**structure that contains possible annotations for us to select to create a relationship here.*/
    private RelCheckResult __rel_checking_result = null;
    /**the name of current working document*/
    private String __filename = null;
    
    private Icon ICON_RELATIONSHIP = null;
    private Icon ICON_SOURCE_ANNOTATION = null;
    private Icon ICON_OBJECT_ANNOTATION = null;
    
    /**
     * Creates new form RelationshipBuilder
     * 
     * @param   _gui
     *          Father component. The instance of class "GUI"; it the main GUI frame of eHOST.
     * 
     * @param   _rel_checking_result
     *          structure that contains possible annotations for us to select to
     *          create a relationship here.
     */
    public RelationshipBuilder(userInterface.GUI _gui, RelCheckResult _rel_checking_result, String _filename) {
        // get external variables
        this.__gui = _gui;
        this.__rel_checking_result = _rel_checking_result;
        this.__filename = _filename;
        // load resource: icons
        loadRes();
        
        // initial components
        initComponents();
        this.jButton_Save.setEnabled( false );        
        // set locations
        this.setLocationRelativeTo(__gui);                               
        
        // disable the father component
        this.__gui.setEnabled(false);
        
        // list data 
        listAnnotations_A( _rel_checking_result );
        
        
        // if only one source annotation and one objective annotation, then we help them select
        if( jList1.getModel().getSize() == 1 )
            this.jList1.setSelectedIndex(0);
        
        
        
    }
    
    
    private void  loadRes(){
        try{
            this.ICON_SOURCE_ANNOTATION = new javax.swing.ImageIcon( RelationshipBuilder.class.getResource("/res/annotation1.jpeg") );
            this.ICON_OBJECT_ANNOTATION = new javax.swing.ImageIcon( RelationshipBuilder.class.getResource("/res/annotation2.jpeg") );
            this.ICON_RELATIONSHIP = new javax.swing.ImageIcon( RelationshipBuilder.class.getResource("/res/links.jpeg") );
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel_button = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jButton_Cancel = new javax.swing.JButton();
        jButton_Save = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel7 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jTextField_SourceAnnotation = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jTextField_ObjectAnnotation = new javax.swing.JTextField();
        jPanel8 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jList4 = new javax.swing.JList();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jList5 = new javax.swing.JList();
        jPanel5 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Select to build relationship");
        setBackground(new java.awt.Color(234, 234, 234));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowDeactivated(java.awt.event.WindowEvent evt) {
                formWindowDeactivated(evt);
            }
        });

        jPanel_button.setBackground(new java.awt.Color(255, 255, 254));
        jPanel_button.setLayout(new java.awt.BorderLayout());

        jPanel3.setBackground(new java.awt.Color(255, 255, 254));
        jPanel3.setLayout(new java.awt.GridLayout(1, 0));

        jButton_Cancel.setText("Cancel");
        jButton_Cancel.setRequestFocusEnabled(false);
        jButton_Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CancelActionPerformed(evt);
            }
        });
        jPanel3.add(jButton_Cancel);

        jButton_Save.setText("Save");
        jButton_Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_SaveActionPerformed(evt);
            }
        });
        jPanel3.add(jButton_Save);

        jPanel_button.add(jPanel3, java.awt.BorderLayout.EAST);

        getContentPane().add(jPanel_button, java.awt.BorderLayout.SOUTH);

        jPanel4.setBackground(new java.awt.Color(245, 245, 244));
        jPanel4.setBorder(javax.swing.BorderFactory.createMatteBorder(0, 2, 2, 2, new java.awt.Color(0, 51, 102)));

        jPanel6.setBackground(new java.awt.Color(244, 244, 244));
        jPanel6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(224, 224, 224), 2));
        jPanel6.setLayout(new java.awt.GridLayout(2, 3, 8, 2));

        jLabel1.setFont(new java.awt.Font("Cambria", 0, 13)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 102, 153));
        jLabel1.setText("Annotation");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jPanel6.add(jLabel1);

        jPanel7.setBackground(new java.awt.Color(244, 244, 244));

        org.jdesktop.layout.GroupLayout jPanel7Layout = new org.jdesktop.layout.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 234, Short.MAX_VALUE)
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 21, Short.MAX_VALUE)
        );

        jPanel6.add(jPanel7);

        jLabel2.setFont(new java.awt.Font("Cambria", 0, 13)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 102, 153));
        jLabel2.setText("Annotation");
        jLabel2.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        jPanel6.add(jLabel2);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153), 2));
        jPanel1.setForeground(new java.awt.Color(0, 102, 255));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jTextField_SourceAnnotation.setEditable(false);
        jTextField_SourceAnnotation.setBorder(null);
        jPanel1.add(jTextField_SourceAnnotation, java.awt.BorderLayout.CENTER);

        jPanel6.add(jPanel1);

        jLabel4.setFont(new java.awt.Font("Cambria", 0, 13)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(0, 102, 153));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("----linked to -->>");
        jPanel6.add(jLabel4);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153), 2));
        jPanel2.setForeground(new java.awt.Color(0, 102, 255));
        jPanel2.setLayout(new java.awt.BorderLayout());

        jTextField_ObjectAnnotation.setEditable(false);
        jTextField_ObjectAnnotation.setBorder(null);
        jPanel2.add(jTextField_ObjectAnnotation, java.awt.BorderLayout.CENTER);

        jPanel6.add(jPanel2);

        jPanel8.setBackground(new java.awt.Color(250, 250, 251));
        jPanel8.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(240, 240, 241), 2));

        jLabel5.setFont(new java.awt.Font("Cambria", 0, 13)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(0, 102, 153));
        jLabel5.setText("<html><b>Step 1.</b><p>Choose first annotation</html>");

        jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153), 2));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(226, 140));

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList1.setBounds(new java.awt.Rectangle(4, 2, 0, 0));
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        jLabel6.setFont(new java.awt.Font("Cambria", 0, 13)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(0, 102, 153));
        jLabel6.setText("<html><b>Step 2.</b><p>Choose destination annotation</html>");

        jScrollPane4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153), 2));
        jScrollPane4.setPreferredSize(new java.awt.Dimension(226, 140));

        jList4.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList4.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList4.setBounds(new java.awt.Rectangle(4, 2, 0, 0));
        jList4.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList4ValueChanged(evt);
            }
        });
        jScrollPane4.setViewportView(jList4);

        jLabel7.setFont(new java.awt.Font("Cambria", 0, 13)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(0, 102, 153));
        jLabel7.setText("<html><b>Step 3.</b><p>Selected a relationship</html>");

        jScrollPane5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153), 2));
        jScrollPane5.setPreferredSize(new java.awt.Dimension(226, 140));

        jList5.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList5.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jList5.setBounds(new java.awt.Rectangle(4, 2, 0, 0));
        jList5.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList5ValueChanged(evt);
            }
        });
        jScrollPane5.setViewportView(jList5);

        org.jdesktop.layout.GroupLayout jPanel8Layout = new org.jdesktop.layout.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel8Layout.createSequentialGroup()
                .add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel8Layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 226, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jPanel8Layout.createSequentialGroup()
                                .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 226, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(18, 18, 18)
                                .add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jLabel7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(jScrollPane5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 226, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))))
                .add(0, 0, Short.MAX_VALUE))
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel8Layout.createSequentialGroup()
                .add(jLabel5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel8Layout.createSequentialGroup()
                        .add(jLabel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel8Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel8Layout.createSequentialGroup()
                                .add(jLabel7, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jScrollPane5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(jScrollPane4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(71, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel8, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 48, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jPanel8, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel4, java.awt.BorderLayout.CENTER);

        jPanel5.setBackground(new java.awt.Color(204, 204, 204));
        jPanel5.setBorder(javax.swing.BorderFactory.createMatteBorder(2, 2, 0, 2, new java.awt.Color(0, 51, 102)));
        jPanel5.setPreferredSize(new java.awt.Dimension(581, 24));
        jPanel5.setLayout(new java.awt.BorderLayout());

        jLabel3.setFont(new java.awt.Font("Cambria", 1, 13)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(51, 0, 51));
        jLabel3.setText("  Link annotations with properly relationship: ");
        jPanel5.add(jLabel3, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel5, java.awt.BorderLayout.NORTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_SaveActionPerformed
        
        // save new relationship
        Annotation annotation = save();
        __gui.setModified();
        __gui.setEnabled(true);
        //this.display_showAnnotationDetail_onEditorPanel(WorkSet.currentAnnotation);
	if( annotation == null )
            __gui.showSelectedAnnotations_inList(0);
        else
            __gui.showSelectedAnnotations_inList( annotation );
        
        __gui.display_removeSymbolIndicators();
        __gui.display_showSymbolIndicators();
        this.dispose();
        
    }//GEN-LAST:event_jButton_SaveActionPerformed

    
    /**save the new relationship to the selected annotation pair.*/
    private Annotation save(){
        this.jButton_Save.setEnabled( false );
        
        int selecteda = jList1.getSelectedIndex();
        if( selecteda < 0 )
            return null;
        
        // ---- 1 ----
        // get the source annotation
        Object selectedObja = jList1.getSelectedValue();
        if( selectedObja == null )
            return null;
        AnnotationObj a_obj = (AnnotationObj) selectedObja;
        if(a_obj==null)
            return null;
        
        // ---- 2.1 ----
        // display annotation text of the source annotation on screen
        Annotation a = a_obj.getAnnotation();
        
        int selected = jList4.getSelectedIndex();
        if( selected < 0 )
            return null;
        
        // ---- 1 ----
        // get the source annotation
        Object selectedObj = jList4.getSelectedValue();
        if( selectedObj == null )
            return null;
        AnnotationObj b_obj = (AnnotationObj) selectedObj;
        if(b_obj==null)
            return null;
        
        // ---- 2.1 ----
        // display annotation text of the source annotation on screen
        this.jTextField_ObjectAnnotation.setText( b_obj.getAnnotation().annotationText );
        Annotation b = b_obj.getAnnotation();
        
        // ---- 3 ----
        // get all possible relationships' names
        Object relObj = this.jList5.getSelectedValue();
        if( relObj == null )
            return null;
        
        String relationshipname = ((RelObj) relObj).getRelName();
        // the relationship name: no null, no empty string
        if(( relationshipname == null)||(relationshipname.trim().length()<1) ){
            return null;
        }
        
        if( GUI.reviewmode == GUI.ReviewMode.adjudicationMode ){
            adjudication.data.AdjudicationDepot.buildRelationship(__filename, relationshipname, a, b);
        }else{
            resultEditor.annotations.Depot.buildRelationship(__filename, relationshipname, a, b);
        }
        
        WorkSet.currentAnnotation = a;
        
        return a;
        
    }
    
    
    
    
    private void jButton_CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_CancelActionPerformed
        __gui.setEnabled(true);
        this.dispose();
    }//GEN-LAST:event_jButton_CancelActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        __gui.setEnabled(true);
        
    }//GEN-LAST:event_formWindowClosing

    private void formWindowDeactivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowDeactivated
        this.toFront();
        this.requestFocus();
        
        // beep
        Tools.beep();
    }//GEN-LAST:event_formWindowDeactivated

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        
        this.jButton_Save.setEnabled( false );
        
        int selected = jList1.getSelectedIndex();
        if( selected < 0 )
            return;
        
        // ---- 1 ----
        // get the source annotation
        Object selectedObj = jList1.getSelectedValue();
        if( selectedObj == null )
            return;
        AnnotationObj a_obj = (AnnotationObj) selectedObj;
        if(a_obj==null)
            return;
        
        // ---- 2.1 ----
        // display annotation text of the source annotation on screen
        this.jTextField_SourceAnnotation.setText( a_obj.getAnnotation().annotationText );
        
        // ---- 2.2 ----
        // get a clone of the selected objective Annotations
        ArrayList<Annotation> objectives = new ArrayList<Annotation>();
        
        for( Annotation ax : this.__rel_checking_result.objectAnnotations ){
            objectives.add(ax);
        }
        
        // pick up annotations who can be linked to current selected source 
        // annotation
        Annotation a = a_obj.getAnnotation();
        for(int i=0; i<objectives.size(); i++){
            Annotation b = objectives.get(i);
            if(b==null)
                continue;
            
            if(env.Parameters.RelationshipSchemas.getPossibleRels(a, b)<=0)
                objectives.set(i, null);
        }        
        
        // clear the selection
        this.jTextField_ObjectAnnotation.setText(null);
        // clear the list of relationship names before relist data of object annotations
        jList5.setListData( new Vector() );
        listAnnotations( this.jList4, objectives);
        
        
        if( jList4.getModel().getSize() == 1 )
            this.jList4.setSelectedIndex(0);
        
    }//GEN-LAST:event_jList1ValueChanged

    private void jList4ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList4ValueChanged
        this.jButton_Save.setEnabled( false );
        
        int selecteda = jList1.getSelectedIndex();
        if( selecteda < 0 )
            return;
        
        // ---- 1 ----
        // get the source annotation
        Object selectedObja = jList1.getSelectedValue();
        if( selectedObja == null )
            return;
        AnnotationObj a_obj = (AnnotationObj) selectedObja;
        if(a_obj==null)
            return;
        
        // ---- 2.1 ----
        // display annotation text of the source annotation on screen
        Annotation a = a_obj.getAnnotation();
        
        int selected = jList4.getSelectedIndex();
        if( selected < 0 )
            return;
        
        // ---- 1 ----
        // get the source annotation
        Object selectedObj = jList4.getSelectedValue();
        if( selectedObj == null )
            return;
        AnnotationObj b_obj = (AnnotationObj) selectedObj;
        if(b_obj==null)
            return;
        
        // ---- 2.1 ----
        // display annotation text of the source annotation on screen
        this.jTextField_ObjectAnnotation.setText( b_obj.getAnnotation().annotationText );
        Annotation b = b_obj.getAnnotation();
        
        // ---- 3 ----
        // get all possible relationships' names
        Object[] relationshipnames = env.Parameters.RelationshipSchemas.getPossibleRelNames(a, b);
                
        // 
        listRels(relationshipnames);
        
        if( jList5.getModel().getSize() == 1 ){
            this.jList5.setSelectedIndex(0);
            jButton_Save.setEnabled(true);
        }else{
            jButton_Save.setEnabled(false);
        }
        
    }//GEN-LAST:event_jList4ValueChanged

    private void jList5ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList5ValueChanged
        // enable/disable the save button based on the selection status of the 
        // list of relationship names
        jButton_Save.setEnabled( (jList5.getSelectedIndex() >= 0) );
    }//GEN-LAST:event_jList5ValueChanged

    /**list names of all possible relationships for current selected annotation
     * pairs. 
     * 
     * @param   relnames
     *          the names of all possible relationships for current selected 
     *          annotation pairs. It's stored in format of a simple array of 
     *          object, and all non-null object can be converted to String to 
     *          get the name.
     */
    private void listRels(Object[] relnames){
        // clear it before load new data
        this.jList5.setListData( new Vector() );
        
        if(relnames == null)
            return;
        
        Vector<RelObj> listdata = new Vector<RelObj>();
        
        for(Object relname_obj : relnames){
            if( relname_obj == null )
                continue;
            String rulName = (String) relname_obj;
            listdata.add( new RelObj(rulName, ICON_RELATIONSHIP));
        }
        
        jList5.setListData( listdata );
        jList5.setCellRenderer( new RelRenderer());
        
    }
    
    /**list source annotations in the list and clear all other list.*/
    private void listAnnotations_A(RelCheckResult _rel_checking_result){
        if((_rel_checking_result == null ) || (_rel_checking_result.needpopup == false))
            return;
        
        
        
        jList1.setListData( new Vector() );
        this.jList4.setListData( new Vector() );
        this.jList5.setListData( new Vector() );
        
        
        Vector<AnnotationObj> listdata = new Vector<AnnotationObj>();
        for(Annotation a : _rel_checking_result.sourceAnnotations){
            if(a!=null){
                listdata.add( new AnnotationObj( a, this.ICON_SOURCE_ANNOTATION) );
            }
        }
        jList1.setListData( listdata );
        jList1.setCellRenderer( new AnnotationRenderer() );
        
        this.jList4.setListData( new Vector() );
        this.jList5.setListData( new Vector() );
        this.jTextField_ObjectAnnotation.setText(null);
        this.jTextField_SourceAnnotation.setText(null);        
    }
    
    /**clear and list annotations in designated list.*/
    private void listAnnotations(JList jlistComponent, ArrayList<Annotation> annotations){
        if(annotations == null ) 
            return;
        
        jlistComponent.setListData( new Vector() );
        
        Vector<AnnotationObj> listdata = new Vector<AnnotationObj>();
        for(Annotation a : annotations){
            if(a!=null){
                listdata.add( new AnnotationObj( a, this.ICON_OBJECT_ANNOTATION) );
            }
        }
        jlistComponent.setListData( listdata );
        jlistComponent.setCellRenderer( new AnnotationRenderer() );
                
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Cancel;
    private javax.swing.JButton jButton_Save;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JList jList1;
    private javax.swing.JList jList4;
    private javax.swing.JList jList5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel_button;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTextField jTextField_ObjectAnnotation;
    private javax.swing.JTextField jTextField_SourceAnnotation;
    // End of variables declaration//GEN-END:variables
}
